package Game.Objects;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Sound;

import GUI.GuiManager;
import GUI.GuiMessenger;
import Game.Items.GameItem;
import Game.Items.Others.GIDoorKey;
import Game.Objects.Creatures.GOCreature;
import Screens.MyScreens.*;
import Tools.Configuration;
import Tools.ResourceManager;
import Tools.SoundManager;
import Tools.Pathfinding.Pathfinding;

/** Door class
 * @author Aikidos
 */
public class GODoor extends GameObject {

	/** Door id */
	private int _id;
	
	public GODoor(ObjectManager objectManager) throws SlickException {
		super(objectManager, "Door");
		
		setSprite(new Image("res/gfx/objects/door.png"));
	}
	
	/** Returns id 
	 * @return Id
	 */
	public int getId() {
		return _id;
	}
	
	/** Set id
	 * @param id - New id
	 */
	public void setId(int id) {
		_id = id;
	}
	
	@Override
	public void draw(Graphics g) {
		super.draw(g);
		
	}

	@Override
	public boolean onCollision(GameObject other, boolean moveOn)
			throws SlickException {
		
		// Player
		if (other.isPlayer()) {
			GOPlayer player = (GOPlayer)other;
			
			// Looking door key
			for (GameItem item : player.getInventory().getItems()) {
				if (item.getClass().getName() == "Game.Items.Others.GIDoorKey") {
					GIDoorKey doorKey = (GIDoorKey)item;
					if (doorKey.getId() == this.getId()) {
						// Show message
						GuiManager.getMessenger().addMessage("[green]You[/green] opened a [yellow]" + getName() + "[/yellow]");
						
						// Set pathfinding and lighting solid maps
						Pathfinding.getPathfindingMap().getSolidMap()[getCoord().x][getCoord().y] = 0;
						_objectManager.getLevel().getLightManager().getSolidMap()[getCoord().x][getCoord().y] = false;
						
						_objectManager.destroyObject(this);
						player.getInventory().deleteItem(doorKey, false);
						
						// Play sound
						SoundManager.play("sfx_door_unlock");
						
						return true;
					}
				}
			}
			
			GuiManager.getMessenger().addMessage("[yellow]" + getName() + "[/yellow] is closed. Need a " +
					"[yellow]Door Key #" + _id + "[/yellow]");
			
			// Play sound
			SoundManager.play("sfx_door_lock");
		}
		
		// Creature
		if (other.isCreature()) {
			GOCreature creature = (GOCreature) other;

			// Looking door key
			for (GameItem item : creature.getInventory().getItems()) {
				if (item.getClass().getName() == "Game.Items.Other.GIDoorKey") {
					GIDoorKey doorKey = (GIDoorKey) item;
					if (doorKey.getId() == this.getId()) {
						
						// Set pathfinding and lighting solid maps
						Pathfinding.getPathfindingMap().getSolidMap()[getCoord().x][getCoord().y] = 0;
						_objectManager.getLevel().getLightManager().getSolidMap()[getCoord().x][getCoord().y] = false;
						
						_objectManager.destroyObject(this);
						creature.getInventory().deleteItem(doorKey, false);
						return true;
					}
				}
			}
		}
	
		return false;
	}
	
}
